package cz.af.portletExams.util;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Company;
import com.liferay.portal.service.CompanyLocalServiceUtil;
import org.apache.log4j.Logger;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import java.util.Enumeration;
import java.util.Map;

public class AFPortletUtil {

    private static final Logger log = Logger.getLogger(AFPortletUtil.class);

    public static void redirectToRoot(ActionResponse response, ActionRequest request) {
//        try {
//            String portletName = (String) request.getAttribute(WebKeys.PORTLET_ID);
//            ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
//            PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(request), portletName, themeDisplay.getLayout().getPlid(),
//                    PortletRequest.RENDER_PHASE);
//            response.sendRedirect(redirectURL.toString().replaceAll(Pattern.quote("?") + ".*", ""));
//        } catch (IOException e) {
//            log.error(e);
//        }
    }

    public static Company getCurrentCompany() {
        try {
//            return CompanyLocalServiceUtil.getCompany("companyId z konfiguraku")
            return CompanyLocalServiceUtil.getCompanies().get(0);
        } catch (SystemException e) {
            log.error(e);
        }
//        try {
//            return CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
//        } catch (PortalException e) {
//            log.error(e);
//        } catch (SystemException e) {
//            log.error(e);
//        }
        return null;
    }

    public static void printAttributesAndParameters(ActionRequest actionRequest) {
        final Enumeration<String> attributeNames = actionRequest.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            final String next = attributeNames.nextElement();
            System.out.println(next + "       " + actionRequest.getAttribute(next));
        }

        System.out.println("PARAMS");
        final Map<String, String[]> parameterMap = actionRequest.getParameterMap();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            System.out.println(entry.getKey() + "       " + entry.getValue());
        }
    }

}
